package com.oron3.lock;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.ofek.applicationbase.utils.Delegate;
import com.oron3.lock.LockDatabaseService.LockServiceBinder;
import com.oron3.lock.ToolController.ToolListener;
import com.oron3.locker.R;

public class LockActivity extends ListActivity implements ServiceConnection,Delegate<ArrayList<Secret>> {
	public final static String KEY_INTENT_EXTRA_UC = "com.oron3.Lock.KEY_INTENT_EXTRA_UC";
	private AES aes;
	private LockDatabaseService service;
	private ToolController toolController;
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final Intent intent = getIntent();
		if (!intent.hasExtra(KEY_INTENT_EXTRA_UC)) {
			startActivity(new Intent(this,LoginActivity.class));
			finish();
		}
		setContentView(R.layout.activity_lock);

		setProgressBarVisibility(true);

		try {
			aes = new AES(intent.getStringExtra(KEY_INTENT_EXTRA_UC));
		} catch (final Exception e) {
			e.printStackTrace();
		} 
		toolController = new ToolController((ViewGroup) findViewById(R.id.tool));
	}

	@Override
	public void onServiceConnected(final ComponentName name, final IBinder service) {
		final LockServiceBinder binder = (LockServiceBinder)service;
		this.service = binder.getLockDatabaseService();
		this.service.getAll(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		unbindService(this);

	}

	@Override
	public void onServiceDisconnected(final ComponentName name) {
	}
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		bindService(new Intent(this,LockDatabaseService.class),this,BIND_AUTO_CREATE);
	}
	@Override
	public void invoke(final ArrayList<Secret> t) {

		for ( final Secret s : t ) {
			try {
				s.text = aes.decrypt(s.text);
				s.title = aes.decrypt(s.title);
			} catch (final Exception e) {
				e.printStackTrace();
			}
		}
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				final SecretListAdapter arrayAdapter = new SecretListAdapter(LockActivity.this, android.R.layout.simple_list_item_1,t);
				toolController.registerToolListener(new ToolListener() {

					@Override
					public void searchRequested(final String query) {

					}

					@Override
					public void onEditChecked(final boolean b) {
						arrayAdapter.setEdit(b);
					}

					@Override
					public void onAddClicked() {
						final Intent intent = new Intent(LockActivity.this,SecretFormActivity.class);
						intent.putExtra(KEY_INTENT_EXTRA_UC, getIntent().getStringExtra(KEY_INTENT_EXTRA_UC));
						startActivity(intent);
					}
				});
				setListAdapter(arrayAdapter);
				getListView().setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(final AdapterView<?> parent, final View view,
							final int position, final long id) {
						final Secret item = (Secret) parent.getItemAtPosition(position);
						final Intent intent = new Intent(LockActivity.this,SecretFormActivity.class);
						intent.putExtra(KEY_INTENT_EXTRA_UC, getIntent().getStringExtra(KEY_INTENT_EXTRA_UC));
						intent.putExtra(SecretFormActivity.KEY_INTENT_EXTRA_SECRET_TO_EDIT,item);
						startActivity(intent);
					}
				});
				setProgressBarVisibility(false);
			}
		});
	}



}
